// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#pragma once

#include <string>
#include <vector>

#include <nebula/types/type.h>
#include <turbo/meta/compare.h>
#include <turbo/base/macros.h>

namespace nebula::compute {

    enum class SortOrder {
        /// Arrange values in increasing order
        Ascending,
        /// Arrange values in decreasing order
        Descending,
    };

    enum class NullPlacement {
        /// Place nulls and NaNs before any non-null values.
        /// NaNs will come after nulls.
        AtStart,
        /// Place nulls and NaNs after any non-null values.
        /// NaNs will come before nulls.
        AtEnd,
    };

    /// \brief One sort key for PartitionNthIndices (TODO) and sort_indices
    class TURBO_EXPORT SortKey : public turbo::EqualityComparable<SortKey> {
    public:
        explicit SortKey(FieldRef target, SortOrder order = SortOrder::Ascending)
                : target(std::move(target)), order(order) {}

        bool equals(const SortKey &other) const;

        std::string to_string() const;

        /// A FieldRef targeting the sort column.
        FieldRef target;
        /// How to order by this sort key.
        SortOrder order;
    };

    class TURBO_EXPORT Ordering : public turbo::EqualityComparable<Ordering> {
    public:
        Ordering(std::vector<SortKey> sort_keys,
                 NullPlacement null_placement = NullPlacement::AtStart)
                : sort_keys_(std::move(sort_keys)), null_placement_(null_placement) {}

        /// true if data ordered by other is also ordered by this
        ///
        /// For example, if data is ordered by [a, b, c] then it is also ordered
        /// by [a, b] but not by [b, c] or [a, b, c, d].
        ///
        /// [a, b].is_suborder_of([a, b, c]) - true
        /// [a, b, c].is_suborder_of([a, b, c]) - true
        /// [b, c].is_suborder_of([a, b, c]) - false
        /// [a, b, c, d].is_suborder_of([a, b, c]) - false
        ///
        /// The implicit ordering is not a suborder of any other ordering and
        /// no other ordering is a suborder of it.  The implicit ordering is not a
        /// suborder of itself.
        ///
        /// The unordered ordering is a suborder of all other orderings but no
        /// other ordering is a suborder of it.  The unordered ordering is a suborder
        /// of itself.
        ///
        /// The unordered ordering is a suborder of the implicit ordering.
        bool is_suborder_of(const Ordering &other) const;

        bool equals(const Ordering &other) const;

        std::string to_string() const;

        bool is_implicit() const { return is_implicit_; }

        bool is_unordered() const { return !is_implicit_ && sort_keys_.empty(); }

        const std::vector<SortKey> &sort_keys() const { return sort_keys_; }

        NullPlacement null_placement() const { return null_placement_; }

        static const Ordering &implicit() {
            static const Ordering kImplicit(true);
            return kImplicit;
        }

        static const Ordering &unordered() {
            static const Ordering kUnordered(false);
            // It is also possible to get an unordered ordering by passing in an empty vector
            // using the normal constructor.  This is ok and useful when ordering comes from user
            // input.
            return kUnordered;
        }

    private:
        explicit Ordering(bool is_implicit)
                : null_placement_(NullPlacement::AtStart), is_implicit_(is_implicit) {}

        /// Column key(s) to order by and how to order by these sort keys.
        std::vector<SortKey> sort_keys_;
        /// Whether nulls and NaNs are placed at the start or at the end
        NullPlacement null_placement_;
        bool is_implicit_ = false;
    };

}  // namespace nebula::compute
